package com.sample.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import com.swak.persistence.DatabaseStorage;
import com.swak.utils.IOUtils;
import com.swak.utils.StringUtils;

/**
 * 数据初始化
 * 
 * @author 618lf
 */
@Component
public class DbInitConfig extends DatabaseStorage implements InitializingBean {

	public DbInitConfig(DataSource dataSource) {
		super(dataSource);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		String tables = IOUtils.readAsText(this.getClass(), "script.sql");
		String[] sqls = StringUtils.split(tables, ";");
		for (String sql : sqls) {
			this.executeUpdate(sql);
		}
	}

	// 不需要输出信息
	
	@Override
	protected void error(Throwable e) {
	}

	@Override
	protected void error(String format, String sql, Throwable e) {
	}
}